#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import Int32
# from origincar_msg.msg import Data  

class NodeSubscribe(Node):
    def __init__(self, name):
        super().__init__(name)
        self.get_logger().info("开始订阅话题")
        # 创建发布者，发布到 /sign_foxglove.data 话题
        self.publisher_ = self.create_publisher(Int32, 'sign_foxglove', 10)

        # 创建一个订阅者，订阅 /sigh4return 话题，消息类型为 Int32
        self.subscription = self.create_subscription(
            Int32,
            '/sign4return',
            self.listener_callback,
            10)
        self.subscription  # 防止未使用的警告

    def listener_callback(self, msg):
        # 创建 Sign 消息实例
        foxglove_msg = Int32()
        # 回调函数，处理接收到的消息
        self.get_logger().info(f'收到消息: {msg.data}')

        if (msg.data == 0):
            self.get_logger().info(f'信号连接正常')
            
            foxglove_msg.data = 0      
             
        if (msg.data == -1):
            self.get_logger().info(f'重置里程计')
            
            foxglove_msg.data = -1
            
        if (msg.data == 6):
            self.get_logger().info(f'结束遥感')

            foxglove_msg.data = 6  

        if (msg.data == 5):
            self.get_logger().info(f'开始遥感')

            foxglove_msg.data = 5

         # 发布消息
        self.publisher_.publish(foxglove_msg)

def main(args=None):
    rclpy.init(args=args)  # 初始化rclpy
    node = NodeSubscribe("sign4return_subscribe")  # 新建一个节点
    rclpy.spin(node)  # 保持节点运行，检测是否收到退出指令（Ctrl+C）
    rclpy.shutdown()  # 关闭rclpy

if __name__ == '__main__':
    main()
